package com.mingte.manghe.common;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @author 28286
 */
@Slf4j
@ApiModel("全局统一返回结果")
public class ResponseResult<T> implements Serializable {

    /**
     * 成功状态码
     */
    private static final int OK = 0;

    /**
     * 失败状态码
     */
    private static final int FAIL = 1;

    /**
     * 系统错误
     */
    private static final int SYS_ERROR = 999;

    /**
     * 未登录态码
     */
    private static final String NO_LOGIN_ERROR_MSG = "请先登录";

    /**
     * 失败
     */
    private static final String IS_FAIL = "失败";


    /**
     * 成功
     */
    private static final String IS_SUCCESS = "成功";

    /**
     * 未登录态码
     */
    private static final String SYS_ERROR_MSG = "系统错误请稍后尝试";

    @ApiModelProperty("状态")
    private int status = OK;

    @ApiModelProperty("信息")
    private String msg;

    @ApiModelProperty("数据")
    private T data;

    private ResponseResult(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private ResponseResult(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private ResponseResult(T data) {
        this.data = data;
    }

    private ResponseResult(){}

    public static void init() {
        log.info("ResponseCodeConst init....");
    }

    public static <T> ResponseResult<T> success(T data){
        return new ResponseResult(data);
    }

    public static <T> ResponseResult<T> success(){
        return new ResponseResult(OK,IS_SUCCESS);
    }

    public static ResponseResult fail(){
        return new ResponseResult(FAIL,IS_FAIL);
    }

    public static ResponseResult fail(String msg){
        return new ResponseResult(FAIL,msg);
    }

    public static <T> ResponseResult<T> fail(String msg,T data){
        return new ResponseResult(FAIL,msg,data);
    }

    public static <T> ResponseResult<T> noLogin(){
        return new ResponseResult(FAIL,NO_LOGIN_ERROR_MSG);
    }

    public static <T> ResponseResult<T> sysError(String msg){
        return new ResponseResult(SYS_ERROR,msg);
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static  void error(HttpServletResponse response,String msg) throws IOException {
        response.reset();
        //设置编码格式
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",1);
        jsonObject.put("msg",msg);
        response.getWriter().write(jsonObject.toJSONString());
    }
}
